package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加菜品
     *
     * @param dishDTO
     */
    @Override
    public void add(DishDTO dishDTO) {
        // 创建一个Dish表对象,把数据拷贝过来
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 调用mapper层
        dishMapper.add(dish);
        // 从dishDTO中获取到菜品口味表的集合,遍历存储到菜品口味表中
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                // 调用菜品口味表的mapper层把数据存储进去
                flavor.setDishId(dish.getId());
                dishFlavorMapper.add(flavor);
            }
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        // 使用分页插件设置查询第几页,每页多少条
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        // 调用mapper层
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional// 开始事务
    public void deleteById(List<Long> ids) {
        // 不能删除启用状态下的菜品
        for (Long id : ids) {
            // 调用mapper层
            Dish dish = dishMapper.findById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                // 如果菜品为起售状态,那么,就不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 不能删除和套餐关联的菜品
        for (Long id : ids) {
            // 查询菜品的id是否被套餐所包含
            SetmealDish setmealDish = setmealDishMapper.findById(id);
            if (setmealDish != null) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //List<Long> setmealDish = setmealDishMapper.findById(ids);
        //        if (setmealDish!= null && setmealDish.size()>0){
        //            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        //        }

        // 删除完菜品后和其相关联的口味也要删除
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteById(id);
        }
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO findById(Long id) {
        // 获取菜品数据
        Dish dish = dishMapper.findById(id);
        // 查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.findById(id);
        // 把菜品数据封装到DishVO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     *
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        // 把dishDTO的属性拷贝到Dish中
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 调用service层修改菜品表信息
        dishMapper.update(dish);
        // 删除原有的菜品口味表
        dishFlavorMapper.deleteById(dishDTO.getId());
        // 重新添加菜品口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0 && flavors != null) {
            for (DishFlavor flavor : flavors) {
                // 先把flavor的id存储进去
                flavor.setDishId(dish.getId());
                dishFlavorMapper.add(flavor);
            }
        }
    }

    /**
     * 修改菜品启用状态
     *
     * @param status
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        // 调用mapper层
        dishMapper.updateStatus(status, id);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> dishVOList = dishMapper.findByCategoryId(categoryId);
        return dishVOList;
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.findById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
